package com.dhcc.bpm.modules.scf.controller.CacheClear;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;

@Slf4j
@RestController
@RequestMapping("/cache")
public class CacheController {
    @Autowired
    private CacheManager cacheManager;

    /**
     * 缓存刷新按钮接口&&加入定时任务每隔60秒执行一次缓存刷新
     *
     * @return
     */
    @PostMapping("/clearAll")
    @Scheduled(fixedRate = 1800000) // 每隔60秒执行一次
    public String clearAllCaches() {
        Collection<String> cacheNames = cacheManager.getCacheNames();
        // 输出缓存列表到日志
        log.info("缓存列表为: {}", cacheNames);
        for (String cacheName : cacheNames) {
            cacheManager.getCache(cacheName).clear();
            log.info("缓存已被清理: {}", cacheName);
        }
        log.info("缓存已刷新！");
        return "缓存已刷新！";
    }
}
